<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>排列组合</title>
    <style type="text/css">
        .up{

        }
        .up textarea,.down textarea{
            width: 100%;
        }
        input#calc,input#num,input#cleanup,input#cleandown{
            font-size: 30px;
        }
    </style>
    <script type="text/javascript">
        function group(nu, groupl, result){
            
            var result = result ? result : [];
            var nul = nu.length;
            var outloopl = nul - groupl;
            
            var nuc = nu.slice(0);
            
            var item = nuc.shift();
            item = item.constructor === Array ? item : [item];
            
            
            (function func(item,nuc){
                var itemc;
                var nucc = nuc.slice(0);
                var margin = groupl- item.length
                
                
                if( margin == 0){
                    result.push(item);
                    return;
                }
                if( margin == 1){
                    for(var j in nuc){
                        itemc = item.slice(0);
                        itemc.push(nuc[j]);
                        result.push(itemc);
                    }           
                }       
                if( margin > 1){
                    itemc = item.slice(0);
                    itemc.push(nucc.shift());
                    func(itemc,nucc);

                    if(item.length + nucc.length >= groupl){
                        func(item,nucc);
                    }
                    
                }
                
            })(item,nuc);
            

            if(nuc.length >= groupl){
                return group(nuc, groupl, result);
            }else{
                return result;
            }
            
        }
        function calc(){
            try{
                var input=document.getElementById("input").value;
                var num=parseInt(document.getElementById("num").value);
                var list = input.split(/[\D*]/m);
                if(num<=list.length){
                    var result = group(list,num);
                    document.getElementById("output").innerHTML = '';

                    for(var k=0;k<result.length;k++){
                        if(document.getElementById("output").innerHTML!=""){
                            document.getElementById("output").innerHTML+=","
                        }
                        document.getElementById("output").innerHTML += "{"+result[k].join(',')+"}";
                    }
                    document.getElementById("count").innerHTML = '分组总数：'+result.length;
                }
                else{
                    document.getElementById("output").innerHTML = '分组数量过大！！！';
                }
            } catch(err) {
                document.getElementById("output").innerHTML = '计算异常！！！';
                return;
            }
        }
        function cleanup(){
            document.getElementById("input").innerHTML = '';
        }
        function cleandown(){
            document.getElementById("output").innerHTML = '';
        }
    </script>
</head>
<body>
    <div class="up">
    <textarea id="input" rows="20">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15</textarea>
    </div>
    <div class="mid">
    <div>
        <input type="text" id="num" value="10"><input type="button" value="计算" id="calc" onclick="calc()" />
        <span id="count"></span>
        <input type="button" value="清空上" id="cleanup" onclick="cleanup()" />
        <input type="button" value="清空下" id="cleandown" onclick="cleandown()" />
    </div>
    </div>
    <div class="down">
    <textarea id="output" rows="20"></textarea>
    </div>
</body>
</html>